{
  "cells": [
    {
      "cell_type": "raw",
      "metadata": {},
      "source": [
        "---\n",
        "title: Intro to value lifecycle\n",
        "sidebar_position: 1\n",
        "description: An introduction to the value lifecycle.\n",
        "css: /static/styles/page-navigation.css\n",
        "website:\n",
        "  open-graph:\n",
        "    image: /static/images/mojo-social-card.png\n",
        "  twitter-card:\n",
        "    image: /static/images/mojo-social-card.png\n",
        "---"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "So far, we've explained how Mojo allows you to build high-performance code that\n",
        "is memory safe _without_ manually managing memory, using Mojo's [ownership\n",
        "model](/mojo/manual/values/ownership.html). However, Mojo is designed for\n",
        "[systems programming](https://en.wikipedia.org/wiki/Systems_programming), which\n",
        "often requires manual memory management for custom data types. So, Mojo lets\n",
        "you do that as you see fit. To be clear, Mojo has no reference counter and no\n",
        "garbage collector.\n",
        "\n",
        "Mojo also has no built-in data types with special privileges. All data types\n",
        "in the standard library (such as [`Bool`](/mojo/stdlib/builtin/bool.html#bool),\n",
        "[`Int`](/mojo/stdlib/builtin/int.html#int), and\n",
        "[`String`](/mojo/stdlib/builtin/string.html#string)) are implemented as\n",
        "[structs](/mojo/manual/structs.html). You can actually write your own\n",
        "replacements for these types by using low-level primitives provided by\n",
        "[MLIR dialects](/mojo/notebooks/BoolMLIR.html).\n",
        "\n",
        "What's great about the Mojo language is that it provides you these low-level\n",
        "tools for systems programming, but within a framework that helps you build\n",
        "things that are safe and easy to use from higher-level programs. That is, you\n",
        "can get under the hood and write all the \"unsafe\" code you want, but as long as\n",
        "you do so in accordance with Mojo's [value\n",
        "semantics](/mojo/manual/values/value-semantics), the programmer instantiating\n",
        "your type/object doesn't need to think about memory management at all, and the\n",
        "behavior will be safe and predictable, thanks to [value\n",
        "ownership](/mojo/manual/values/ownership.html).\n",
        "\n",
        "In summary, it's the responsibility of the type author to manage the memory and\n",
        "resources for each value type, by implementing specific lifecycle methods, such\n",
        "as the constructor, copy constructor, move constructor, and destructor, as\n",
        "necessary. Mojo doesn't create any constructors by default, although it does\n",
        "add a trivial, no-op destructor for types that don't define their own.\n",
        "\n",
        "In the following pages, we'll explain exactly how to define these lifecycle\n",
        "methods in accordance with value semantics so your types play nicely with value\n",
        "ownership."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Lifecycles and lifetimes\n",
        "\n",
        "First, let's clarify some terminology:\n",
        "\n",
        "- The \"lifecycle\" of a value is defined by various [dunder\n",
        "methods](/mojo/manual/structs.html#special-methods) in a struct.\n",
        "Each lifecycle event is handled by a different method,\n",
        "such as the constructor (`__init__()`), the destructor (`__del__()`), the copy\n",
        "constructor (`__copyinit__()`), and the move constructor (`__moveinit__()`).\n",
        "All values that are declared with the same type have the same lifecycle.\n",
        "\n",
        "- The \"lifetime\" of a value is defined by the span of time during program\n",
        "execution in which each value is considered valid. The life of a value begins\n",
        "when it is initialized and ends when it is destroyed, which generally (but not\n",
        "always) spans from `__init__()` to `__del__()`. No two values have the exact\n",
        "same lifetime, because every value is created and destroyed at a different\n",
        "point in time (even if the difference is imperceivable).\n",
        "\n",
        "The life of a value in Mojo begins when a variable is initialized and continues\n",
        "up until the value is last used, at which point Mojo destroys it. Mojo destroys\n",
        "every value/object as soon as it's no longer used, using an “as soon as\n",
        "possible” (ASAP) destruction policy that runs after every sub-expression.\n",
        "\n",
        "As you might imagine, keeping track of a value's lifetime can be difficult if a\n",
        "value is shared across functions many times during the life of a program.\n",
        "However, Mojo makes this predictable partly through its [value\n",
        "semantics](/mojo/manual/values/value-semantics.html) and [value\n",
        "ownership](/mojo/manual/values/ownership.html) (both prerequisite readings for\n",
        "the following sections). The final piece of the puzzle for lifetime management\n",
        "is the value lifecycle: every value (defined in a struct) needs to implement\n",
        "key lifecycle methods that define how a value is created and destroyed."
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Mojo",
      "language": "mojo",
      "name": "mojo-jupyter-kernel"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "mojo"
      },
      "file_extension": ".mojo",
      "mimetype": "text/x-mojo",
      "name": "mojo"
    },
    "orig_nbformat": 4
  },
  "nbformat": 4,
  "nbformat_minor": 2
}
